﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int row, column;
        const int size_cell = 20;
        int count_way;
        public Form1()
        {
            InitializeComponent();
        }

        private void personToolStripMenuItem_Click(object sender, EventArgs e)
        {
            count_way = 0;
            row = column = 30;
            this.Size = new Size((column+1) * size_cell, (row + 1) * size_cell + 30);

            for (int j = 0; j < column; j++)
            {
                for (int i = 0; i < row; i++)
                {
                    MyButton bt = new MyButton(i, j);
                    bt.Size = new Size(size_cell, size_cell);
                    bt.Location = new Point(i * size_cell, j * size_cell + 30);
                    Controls.Add(bt);
                    bt.Click += bt_Click;
                }
            }
        }

 
        private void bt_Click(object sender, EventArgs e)
        {
            Point temp = ((MyButton)sender).Location;
            temp.X++; temp.Y++;
            Label Lbl = new Label();
            Lbl.Location = temp;
            Lbl.Size = ((MyButton)sender).Size;
            Controls.Remove(((MyButton)sender));
            Lbl.TextAlign = ContentAlignment.MiddleCenter;
            Lbl.Font = new Font("Times New Roman", 12, FontStyle.Bold);
            Lbl.BorderStyle = BorderStyle.Fixed3D;
            if (count_way % 2 == 0)
            {
                Lbl.Text = "X";
                Lbl.ForeColor = Color.Red;
                Lbl.BackColor = Color.Orange;
            }
            else
            {
                Lbl.Text = "O";
                Lbl.ForeColor = Color.Blue;
                Lbl.BackColor = Color.Cyan;
            }
            count_way++;
            Controls.Add(Lbl);
        }
    }
    public class MyButton : Button
    {
        public MyButton(int X, int Y){
            Location_X = X;
            Location_Y = Y;
        }
        private int Location_X; public int get_X() { return Location_X; }
        private int Location_Y; public int get_Y() { return Location_Y; }
    }
}

